﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using SIMS.Shared.DTO;
using SIMS.Shared.Models;
using SIMS.SqlSugarWebApi.Models;
using SIMS.SqlSugarWebApi.ServiceInterface;

namespace SIMS.SqlSugarWebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class StudentController : ControllerBase
    {
        private readonly IStudentService _service;
        private readonly IMapper _mapper;

        public StudentController(IStudentService service, IMapper mapper)
        {
            _service = service;
           _mapper = mapper;
        }

        [HttpPost]
        public async Task<IActionResult> AddStudent(StudentDTO s)
        {
            var student = _mapper.Map<Student>(s);
            var data = await _service.AddEntity(student);
            var res = new ResponseResultModel() { data = data };
            return Ok(res);
        }

        [HttpDelete]
        public async Task<IActionResult> DeleteStudent(int id)
        {
            var data = await _service.DeleteEntity(id);
            var res = new ResponseResultModel() { data = data };
            return Ok(res);
        }

        [HttpGet]
        public async Task<IActionResult> GetStudent(int id)
        {
            var data = await _service.GetEntity(id);
            var res = new ResponseResultModel() { data = data };
            return Ok(res);
        }

        [HttpPut]
        public async Task<IActionResult> UpdateStudent(StudentDTO s)
        {
            var student = _mapper.Map<Student>(s);
            var data = await _service.UpdateEntity(student);
            var res = new ResponseResultModel() { data = data };
            return Ok(res);
        }

        [HttpGet]
        public async Task<IActionResult> GetStudents(int pageNum, int pageSize, string? no = null, string? name = null)
        {
            var data = await _service.GetStudents(no, name, pageNum, pageSize);
            var res = new ResponseResultModel() { data = data };
            return Ok(res);
        }

        [HttpGet]
        public async Task<IActionResult> GetStudentsByClasses(int classId)
        {
            var data = await _service.GetStudentsByClasses(classId);
            var res = new ResponseResultModel() { data = data };
            return Ok(res);
        }
    }
}
